package pe.edu.upc.e41a.javasoft.jsf;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;
import javax.faces.validator.LengthValidator;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.calendar.Calendar;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.message.Message;
import org.primefaces.component.spinner.Spinner;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.edu.upc.e41a.javasoft.domain.Categoria;
import pe.edu.upc.e41a.javasoft.domain.Imagen;
import pe.edu.upc.e41a.javasoft.domain.Publicacion;
import pe.edu.upc.e41a.javasoft.domain.Transaccion;
import pe.edu.upc.e41a.javasoft.domain.Usuario;
import pe.edu.upc.e41a.javasoft.domain.Visita;
import pe.edu.upc.e41a.javasoft.jsf.converter.CategoriaConverter;
import pe.edu.upc.e41a.javasoft.jsf.converter.UsuarioConverter;
import pe.edu.upc.e41a.javasoft.service.CategoriaService;
import pe.edu.upc.e41a.javasoft.service.PublicacionService;
import pe.edu.upc.e41a.javasoft.service.UsuarioService;

@Configurable
@ManagedBean(name = "publicacionBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = Publicacion.class, beanName = "publicacionBean")
public class PublicacionBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
    PublicacionService publicacionService;

	@Autowired
    CategoriaService categoriaService;

	@Autowired
    UsuarioService usuarioService;

	private String name = "Publicacions";

	private Publicacion publicacion;

	private List<Publicacion> allPublicacions;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<Imagen> selectedImagens;

	private List<Transaccion> selectedTransaccions;

	private List<Visita> selectedVisitas;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("titulo");
        columns.add("descripcion");
        columns.add("tipo");
        columns.add("cantidad");
        columns.add("precio");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Publicacion> getAllPublicacions() {
        return allPublicacions;
    }

	public void setAllPublicacions(List<Publicacion> allPublicacions) {
        this.allPublicacions = allPublicacions;
    }

	public String findAllPublicacions() {
        allPublicacions = publicacionService.findAllPublicacions();
        dataVisible = !allPublicacions.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText imagensCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        imagensCreateOutput.setId("imagensCreateOutput");
        imagensCreateOutput.setValue("Imagens:   ");
        htmlPanelGrid.getChildren().add(imagensCreateOutput);
        
        HtmlOutputText imagensCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        imagensCreateInput.setId("imagensCreateInput");
        imagensCreateInput.setValue("This relationship is managed from the Imagen side");
        htmlPanelGrid.getChildren().add(imagensCreateInput);
        
        Message imagensCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        imagensCreateInputMessage.setId("imagensCreateInputMessage");
        imagensCreateInputMessage.setFor("imagensCreateInput");
        imagensCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(imagensCreateInputMessage);
        
        HtmlOutputText transaccionsCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsCreateOutput.setId("transaccionsCreateOutput");
        transaccionsCreateOutput.setValue("Transaccions:   ");
        htmlPanelGrid.getChildren().add(transaccionsCreateOutput);
        
        HtmlOutputText transaccionsCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsCreateInput.setId("transaccionsCreateInput");
        transaccionsCreateInput.setValue("This relationship is managed from the Transaccion side");
        htmlPanelGrid.getChildren().add(transaccionsCreateInput);
        
        Message transaccionsCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        transaccionsCreateInputMessage.setId("transaccionsCreateInputMessage");
        transaccionsCreateInputMessage.setFor("transaccionsCreateInput");
        transaccionsCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(transaccionsCreateInputMessage);
        
        HtmlOutputText visitasCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        visitasCreateOutput.setId("visitasCreateOutput");
        visitasCreateOutput.setValue("Visitas:   ");
        htmlPanelGrid.getChildren().add(visitasCreateOutput);
        
        HtmlOutputText visitasCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        visitasCreateInput.setId("visitasCreateInput");
        visitasCreateInput.setValue("This relationship is managed from the Visita side");
        htmlPanelGrid.getChildren().add(visitasCreateInput);
        
        Message visitasCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        visitasCreateInputMessage.setId("visitasCreateInputMessage");
        visitasCreateInputMessage.setFor("visitasCreateInput");
        visitasCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(visitasCreateInputMessage);
        
        HtmlOutputText categoriaCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        categoriaCreateOutput.setId("categoriaCreateOutput");
        categoriaCreateOutput.setValue("Categoria:   ");
        htmlPanelGrid.getChildren().add(categoriaCreateOutput);
        
        AutoComplete categoriaCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        categoriaCreateInput.setId("categoriaCreateInput");
        categoriaCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.categoria}", Categoria.class));
        categoriaCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{publicacionBean.completeCategoria}", List.class, new Class[] { String.class }));
        categoriaCreateInput.setDropdown(true);
        categoriaCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "categoria", String.class));
        categoriaCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{categoria.nombre} #{categoria.descripcion}", String.class));
        categoriaCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{categoria}", Categoria.class));
        categoriaCreateInput.setConverter(new CategoriaConverter());
        categoriaCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(categoriaCreateInput);
        
        Message categoriaCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        categoriaCreateInputMessage.setId("categoriaCreateInputMessage");
        categoriaCreateInputMessage.setFor("categoriaCreateInput");
        categoriaCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(categoriaCreateInputMessage);
        
        HtmlOutputText usuarioCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioCreateOutput.setId("usuarioCreateOutput");
        usuarioCreateOutput.setValue("Usuario:   ");
        htmlPanelGrid.getChildren().add(usuarioCreateOutput);
        
        AutoComplete usuarioCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        usuarioCreateInput.setId("usuarioCreateInput");
        usuarioCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.usuario}", Usuario.class));
        usuarioCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{publicacionBean.completeUsuario}", List.class, new Class[] { String.class }));
        usuarioCreateInput.setDropdown(true);
        usuarioCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "usuario", String.class));
        usuarioCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{usuario.nombres} #{usuario.apellidoPaterno} #{usuario.apellidoMaterno} #{usuario.sexo}", String.class));
        usuarioCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{usuario}", Usuario.class));
        usuarioCreateInput.setConverter(new UsuarioConverter());
        usuarioCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(usuarioCreateInput);
        
        Message usuarioCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        usuarioCreateInputMessage.setId("usuarioCreateInputMessage");
        usuarioCreateInputMessage.setFor("usuarioCreateInput");
        usuarioCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(usuarioCreateInputMessage);
        
        HtmlOutputText tituloCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tituloCreateOutput.setId("tituloCreateOutput");
        tituloCreateOutput.setValue("Titulo: * ");
        htmlPanelGrid.getChildren().add(tituloCreateOutput);
        
        InputTextarea tituloCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        tituloCreateInput.setId("tituloCreateInput");
        tituloCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.titulo}", String.class));
        LengthValidator tituloCreateInputValidator = new LengthValidator();
        tituloCreateInputValidator.setMaximum(100);
        tituloCreateInput.addValidator(tituloCreateInputValidator);
        tituloCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(tituloCreateInput);
        
        Message tituloCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tituloCreateInputMessage.setId("tituloCreateInputMessage");
        tituloCreateInputMessage.setFor("tituloCreateInput");
        tituloCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tituloCreateInputMessage);
        
        HtmlOutputText descripcionCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionCreateOutput.setId("descripcionCreateOutput");
        descripcionCreateOutput.setValue("Descripcion:   ");
        htmlPanelGrid.getChildren().add(descripcionCreateOutput);
        
        InputText descripcionCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        descripcionCreateInput.setId("descripcionCreateInput");
        descripcionCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.descripcion}", String.class));
        htmlPanelGrid.getChildren().add(descripcionCreateInput);
        
        Message descripcionCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        descripcionCreateInputMessage.setId("descripcionCreateInputMessage");
        descripcionCreateInputMessage.setFor("descripcionCreateInput");
        descripcionCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(descripcionCreateInputMessage);
        
        HtmlOutputText tipoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoCreateOutput.setId("tipoCreateOutput");
        tipoCreateOutput.setValue("Tipo: * ");
        htmlPanelGrid.getChildren().add(tipoCreateOutput);
        
        InputText tipoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        tipoCreateInput.setId("tipoCreateInput");
        tipoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.tipo}", Character.class));
        tipoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(tipoCreateInput);
        
        Message tipoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tipoCreateInputMessage.setId("tipoCreateInputMessage");
        tipoCreateInputMessage.setFor("tipoCreateInput");
        tipoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tipoCreateInputMessage);
        
        HtmlOutputText cantidadCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        cantidadCreateOutput.setId("cantidadCreateOutput");
        cantidadCreateOutput.setValue("Cantidad: * ");
        htmlPanelGrid.getChildren().add(cantidadCreateOutput);
        
        Spinner cantidadCreateInput = (Spinner) application.createComponent(Spinner.COMPONENT_TYPE);
        cantidadCreateInput.setId("cantidadCreateInput");
        cantidadCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.cantidad}", Integer.class));
        cantidadCreateInput.setRequired(true);
        
        htmlPanelGrid.getChildren().add(cantidadCreateInput);
        
        Message cantidadCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        cantidadCreateInputMessage.setId("cantidadCreateInputMessage");
        cantidadCreateInputMessage.setFor("cantidadCreateInput");
        cantidadCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(cantidadCreateInputMessage);
        
        HtmlOutputText precioCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        precioCreateOutput.setId("precioCreateOutput");
        precioCreateOutput.setValue("Precio: * ");
        htmlPanelGrid.getChildren().add(precioCreateOutput);
        
        InputText precioCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        precioCreateInput.setId("precioCreateInput");
        precioCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.precio}", BigDecimal.class));
        precioCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(precioCreateInput);
        
        Message precioCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        precioCreateInputMessage.setId("precioCreateInputMessage");
        precioCreateInputMessage.setFor("precioCreateInput");
        precioCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(precioCreateInputMessage);
        
        HtmlOutputText fechaCreacionCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaCreacionCreateOutput.setId("fechaCreacionCreateOutput");
        fechaCreacionCreateOutput.setValue("Fecha Creacion: * ");
        htmlPanelGrid.getChildren().add(fechaCreacionCreateOutput);
        
        Calendar fechaCreacionCreateInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        fechaCreacionCreateInput.setId("fechaCreacionCreateInput");
        fechaCreacionCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.fechaCreacion}", Date.class));
        fechaCreacionCreateInput.setNavigator(true);
        fechaCreacionCreateInput.setEffect("slideDown");
        fechaCreacionCreateInput.setPattern("dd/MM/yyyy");
        fechaCreacionCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(fechaCreacionCreateInput);
        
        Message fechaCreacionCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        fechaCreacionCreateInputMessage.setId("fechaCreacionCreateInputMessage");
        fechaCreacionCreateInputMessage.setFor("fechaCreacionCreateInput");
        fechaCreacionCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(fechaCreacionCreateInputMessage);
        
        HtmlOutputText duracionCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        duracionCreateOutput.setId("duracionCreateOutput");
        duracionCreateOutput.setValue("Duracion: * ");
        htmlPanelGrid.getChildren().add(duracionCreateOutput);
        
        Spinner duracionCreateInput = (Spinner) application.createComponent(Spinner.COMPONENT_TYPE);
        duracionCreateInput.setId("duracionCreateInput");
        duracionCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.duracion}", Integer.class));
        duracionCreateInput.setRequired(true);
        
        htmlPanelGrid.getChildren().add(duracionCreateInput);
        
        Message duracionCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        duracionCreateInputMessage.setId("duracionCreateInputMessage");
        duracionCreateInputMessage.setFor("duracionCreateInput");
        duracionCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(duracionCreateInputMessage);
        
        HtmlOutputText estadoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        estadoCreateOutput.setId("estadoCreateOutput");
        estadoCreateOutput.setValue("Estado: * ");
        htmlPanelGrid.getChildren().add(estadoCreateOutput);
        
        InputText estadoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        estadoCreateInput.setId("estadoCreateInput");
        estadoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.estado}", Character.class));
        estadoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(estadoCreateInput);
        
        Message estadoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        estadoCreateInputMessage.setId("estadoCreateInputMessage");
        estadoCreateInputMessage.setFor("estadoCreateInput");
        estadoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(estadoCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText imagensEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        imagensEditOutput.setId("imagensEditOutput");
        imagensEditOutput.setValue("Imagens:   ");
        htmlPanelGrid.getChildren().add(imagensEditOutput);
        
        HtmlOutputText imagensEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        imagensEditInput.setId("imagensEditInput");
        imagensEditInput.setValue("This relationship is managed from the Imagen side");
        htmlPanelGrid.getChildren().add(imagensEditInput);
        
        Message imagensEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        imagensEditInputMessage.setId("imagensEditInputMessage");
        imagensEditInputMessage.setFor("imagensEditInput");
        imagensEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(imagensEditInputMessage);
        
        HtmlOutputText transaccionsEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsEditOutput.setId("transaccionsEditOutput");
        transaccionsEditOutput.setValue("Transaccions:   ");
        htmlPanelGrid.getChildren().add(transaccionsEditOutput);
        
        HtmlOutputText transaccionsEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsEditInput.setId("transaccionsEditInput");
        transaccionsEditInput.setValue("This relationship is managed from the Transaccion side");
        htmlPanelGrid.getChildren().add(transaccionsEditInput);
        
        Message transaccionsEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        transaccionsEditInputMessage.setId("transaccionsEditInputMessage");
        transaccionsEditInputMessage.setFor("transaccionsEditInput");
        transaccionsEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(transaccionsEditInputMessage);
        
        HtmlOutputText visitasEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        visitasEditOutput.setId("visitasEditOutput");
        visitasEditOutput.setValue("Visitas:   ");
        htmlPanelGrid.getChildren().add(visitasEditOutput);
        
        HtmlOutputText visitasEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        visitasEditInput.setId("visitasEditInput");
        visitasEditInput.setValue("This relationship is managed from the Visita side");
        htmlPanelGrid.getChildren().add(visitasEditInput);
        
        Message visitasEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        visitasEditInputMessage.setId("visitasEditInputMessage");
        visitasEditInputMessage.setFor("visitasEditInput");
        visitasEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(visitasEditInputMessage);
        
        HtmlOutputText categoriaEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        categoriaEditOutput.setId("categoriaEditOutput");
        categoriaEditOutput.setValue("Categoria:   ");
        htmlPanelGrid.getChildren().add(categoriaEditOutput);
        
        AutoComplete categoriaEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        categoriaEditInput.setId("categoriaEditInput");
        categoriaEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.categoria}", Categoria.class));
        categoriaEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{publicacionBean.completeCategoria}", List.class, new Class[] { String.class }));
        categoriaEditInput.setDropdown(true);
        categoriaEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "categoria", String.class));
        categoriaEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{categoria.nombre} #{categoria.descripcion}", String.class));
        categoriaEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{categoria}", Categoria.class));
        categoriaEditInput.setConverter(new CategoriaConverter());
        categoriaEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(categoriaEditInput);
        
        Message categoriaEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        categoriaEditInputMessage.setId("categoriaEditInputMessage");
        categoriaEditInputMessage.setFor("categoriaEditInput");
        categoriaEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(categoriaEditInputMessage);
        
        HtmlOutputText usuarioEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioEditOutput.setId("usuarioEditOutput");
        usuarioEditOutput.setValue("Usuario:   ");
        htmlPanelGrid.getChildren().add(usuarioEditOutput);
        
        AutoComplete usuarioEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        usuarioEditInput.setId("usuarioEditInput");
        usuarioEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.usuario}", Usuario.class));
        usuarioEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{publicacionBean.completeUsuario}", List.class, new Class[] { String.class }));
        usuarioEditInput.setDropdown(true);
        usuarioEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "usuario", String.class));
        usuarioEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{usuario.nombres} #{usuario.apellidoPaterno} #{usuario.apellidoMaterno} #{usuario.sexo}", String.class));
        usuarioEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{usuario}", Usuario.class));
        usuarioEditInput.setConverter(new UsuarioConverter());
        usuarioEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(usuarioEditInput);
        
        Message usuarioEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        usuarioEditInputMessage.setId("usuarioEditInputMessage");
        usuarioEditInputMessage.setFor("usuarioEditInput");
        usuarioEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(usuarioEditInputMessage);
        
        HtmlOutputText tituloEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tituloEditOutput.setId("tituloEditOutput");
        tituloEditOutput.setValue("Titulo: * ");
        htmlPanelGrid.getChildren().add(tituloEditOutput);
        
        InputTextarea tituloEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        tituloEditInput.setId("tituloEditInput");
        tituloEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.titulo}", String.class));
        LengthValidator tituloEditInputValidator = new LengthValidator();
        tituloEditInputValidator.setMaximum(100);
        tituloEditInput.addValidator(tituloEditInputValidator);
        tituloEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(tituloEditInput);
        
        Message tituloEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tituloEditInputMessage.setId("tituloEditInputMessage");
        tituloEditInputMessage.setFor("tituloEditInput");
        tituloEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tituloEditInputMessage);
        
        HtmlOutputText descripcionEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionEditOutput.setId("descripcionEditOutput");
        descripcionEditOutput.setValue("Descripcion:   ");
        htmlPanelGrid.getChildren().add(descripcionEditOutput);
        
        InputText descripcionEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        descripcionEditInput.setId("descripcionEditInput");
        descripcionEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.descripcion}", String.class));
        htmlPanelGrid.getChildren().add(descripcionEditInput);
        
        Message descripcionEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        descripcionEditInputMessage.setId("descripcionEditInputMessage");
        descripcionEditInputMessage.setFor("descripcionEditInput");
        descripcionEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(descripcionEditInputMessage);
        
        HtmlOutputText tipoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoEditOutput.setId("tipoEditOutput");
        tipoEditOutput.setValue("Tipo: * ");
        htmlPanelGrid.getChildren().add(tipoEditOutput);
        
        InputText tipoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        tipoEditInput.setId("tipoEditInput");
        tipoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.tipo}", Character.class));
        tipoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(tipoEditInput);
        
        Message tipoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tipoEditInputMessage.setId("tipoEditInputMessage");
        tipoEditInputMessage.setFor("tipoEditInput");
        tipoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tipoEditInputMessage);
        
        HtmlOutputText cantidadEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        cantidadEditOutput.setId("cantidadEditOutput");
        cantidadEditOutput.setValue("Cantidad: * ");
        htmlPanelGrid.getChildren().add(cantidadEditOutput);
        
        Spinner cantidadEditInput = (Spinner) application.createComponent(Spinner.COMPONENT_TYPE);
        cantidadEditInput.setId("cantidadEditInput");
        cantidadEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.cantidad}", Integer.class));
        cantidadEditInput.setRequired(true);
        
        htmlPanelGrid.getChildren().add(cantidadEditInput);
        
        Message cantidadEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        cantidadEditInputMessage.setId("cantidadEditInputMessage");
        cantidadEditInputMessage.setFor("cantidadEditInput");
        cantidadEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(cantidadEditInputMessage);
        
        HtmlOutputText precioEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        precioEditOutput.setId("precioEditOutput");
        precioEditOutput.setValue("Precio: * ");
        htmlPanelGrid.getChildren().add(precioEditOutput);
        
        InputText precioEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        precioEditInput.setId("precioEditInput");
        precioEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.precio}", BigDecimal.class));
        precioEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(precioEditInput);
        
        Message precioEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        precioEditInputMessage.setId("precioEditInputMessage");
        precioEditInputMessage.setFor("precioEditInput");
        precioEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(precioEditInputMessage);
        
        HtmlOutputText fechaCreacionEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaCreacionEditOutput.setId("fechaCreacionEditOutput");
        fechaCreacionEditOutput.setValue("Fecha Creacion: * ");
        htmlPanelGrid.getChildren().add(fechaCreacionEditOutput);
        
        Calendar fechaCreacionEditInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        fechaCreacionEditInput.setId("fechaCreacionEditInput");
        fechaCreacionEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.fechaCreacion}", Date.class));
        fechaCreacionEditInput.setNavigator(true);
        fechaCreacionEditInput.setEffect("slideDown");
        fechaCreacionEditInput.setPattern("dd/MM/yyyy");
        fechaCreacionEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(fechaCreacionEditInput);
        
        Message fechaCreacionEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        fechaCreacionEditInputMessage.setId("fechaCreacionEditInputMessage");
        fechaCreacionEditInputMessage.setFor("fechaCreacionEditInput");
        fechaCreacionEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(fechaCreacionEditInputMessage);
        
        HtmlOutputText duracionEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        duracionEditOutput.setId("duracionEditOutput");
        duracionEditOutput.setValue("Duracion: * ");
        htmlPanelGrid.getChildren().add(duracionEditOutput);
        
        Spinner duracionEditInput = (Spinner) application.createComponent(Spinner.COMPONENT_TYPE);
        duracionEditInput.setId("duracionEditInput");
        duracionEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.duracion}", Integer.class));
        duracionEditInput.setRequired(true);
        
        htmlPanelGrid.getChildren().add(duracionEditInput);
        
        Message duracionEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        duracionEditInputMessage.setId("duracionEditInputMessage");
        duracionEditInputMessage.setFor("duracionEditInput");
        duracionEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(duracionEditInputMessage);
        
        HtmlOutputText estadoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        estadoEditOutput.setId("estadoEditOutput");
        estadoEditOutput.setValue("Estado: * ");
        htmlPanelGrid.getChildren().add(estadoEditOutput);
        
        InputText estadoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        estadoEditInput.setId("estadoEditInput");
        estadoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.estado}", Character.class));
        estadoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(estadoEditInput);
        
        Message estadoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        estadoEditInputMessage.setId("estadoEditInputMessage");
        estadoEditInputMessage.setFor("estadoEditInput");
        estadoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(estadoEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText imagensLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        imagensLabel.setId("imagensLabel");
        imagensLabel.setValue("Imagens:   ");
        htmlPanelGrid.getChildren().add(imagensLabel);
        
        HtmlOutputText imagensValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        imagensValue.setId("imagensValue");
        imagensValue.setValue("This relationship is managed from the Imagen side");
        htmlPanelGrid.getChildren().add(imagensValue);
        
        HtmlOutputText transaccionsLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsLabel.setId("transaccionsLabel");
        transaccionsLabel.setValue("Transaccions:   ");
        htmlPanelGrid.getChildren().add(transaccionsLabel);
        
        HtmlOutputText transaccionsValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsValue.setId("transaccionsValue");
        transaccionsValue.setValue("This relationship is managed from the Transaccion side");
        htmlPanelGrid.getChildren().add(transaccionsValue);
        
        HtmlOutputText visitasLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        visitasLabel.setId("visitasLabel");
        visitasLabel.setValue("Visitas:   ");
        htmlPanelGrid.getChildren().add(visitasLabel);
        
        HtmlOutputText visitasValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        visitasValue.setId("visitasValue");
        visitasValue.setValue("This relationship is managed from the Visita side");
        htmlPanelGrid.getChildren().add(visitasValue);
        
        HtmlOutputText categoriaLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        categoriaLabel.setId("categoriaLabel");
        categoriaLabel.setValue("Categoria:   ");
        htmlPanelGrid.getChildren().add(categoriaLabel);
        
        HtmlOutputText categoriaValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        categoriaValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.categoria}", Categoria.class));
        categoriaValue.setConverter(new CategoriaConverter());
        htmlPanelGrid.getChildren().add(categoriaValue);
        
        HtmlOutputText usuarioLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioLabel.setId("usuarioLabel");
        usuarioLabel.setValue("Usuario:   ");
        htmlPanelGrid.getChildren().add(usuarioLabel);
        
        HtmlOutputText usuarioValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.usuario}", Usuario.class));
        usuarioValue.setConverter(new UsuarioConverter());
        htmlPanelGrid.getChildren().add(usuarioValue);
        
        HtmlOutputText tituloLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tituloLabel.setId("tituloLabel");
        tituloLabel.setValue("Titulo:   ");
        htmlPanelGrid.getChildren().add(tituloLabel);
        
        InputTextarea tituloValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        tituloValue.setId("tituloValue");
        tituloValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.titulo}", String.class));
        tituloValue.setReadonly(true);
        tituloValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(tituloValue);
        
        HtmlOutputText descripcionLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionLabel.setId("descripcionLabel");
        descripcionLabel.setValue("Descripcion:   ");
        htmlPanelGrid.getChildren().add(descripcionLabel);
        
        HtmlOutputText descripcionValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionValue.setId("descripcionValue");
        descripcionValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.descripcion}", String.class));
        htmlPanelGrid.getChildren().add(descripcionValue);
        
        HtmlOutputText tipoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoLabel.setId("tipoLabel");
        tipoLabel.setValue("Tipo:   ");
        htmlPanelGrid.getChildren().add(tipoLabel);
        
        HtmlOutputText tipoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.tipo}", String.class));
        htmlPanelGrid.getChildren().add(tipoValue);
        
        HtmlOutputText cantidadLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        cantidadLabel.setId("cantidadLabel");
        cantidadLabel.setValue("Cantidad:   ");
        htmlPanelGrid.getChildren().add(cantidadLabel);
        
        HtmlOutputText cantidadValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        cantidadValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.cantidad}", String.class));
        htmlPanelGrid.getChildren().add(cantidadValue);
        
        HtmlOutputText precioLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        precioLabel.setId("precioLabel");
        precioLabel.setValue("Precio:   ");
        htmlPanelGrid.getChildren().add(precioLabel);
        
        HtmlOutputText precioValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        precioValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.precio}", String.class));
        htmlPanelGrid.getChildren().add(precioValue);
        
        HtmlOutputText fechaCreacionLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaCreacionLabel.setId("fechaCreacionLabel");
        fechaCreacionLabel.setValue("Fecha Creacion:   ");
        htmlPanelGrid.getChildren().add(fechaCreacionLabel);
        
        HtmlOutputText fechaCreacionValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaCreacionValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.fechaCreacion}", Date.class));
        DateTimeConverter fechaCreacionValueConverter = (DateTimeConverter) application.createConverter(DateTimeConverter.CONVERTER_ID);
        fechaCreacionValueConverter.setPattern("dd/MM/yyyy");
        fechaCreacionValue.setConverter(fechaCreacionValueConverter);
        htmlPanelGrid.getChildren().add(fechaCreacionValue);
        
        HtmlOutputText duracionLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        duracionLabel.setId("duracionLabel");
        duracionLabel.setValue("Duracion:   ");
        htmlPanelGrid.getChildren().add(duracionLabel);
        
        HtmlOutputText duracionValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        duracionValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.duracion}", String.class));
        htmlPanelGrid.getChildren().add(duracionValue);
        
        HtmlOutputText estadoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        estadoLabel.setId("estadoLabel");
        estadoLabel.setValue("Estado:   ");
        htmlPanelGrid.getChildren().add(estadoLabel);
        
        HtmlOutputText estadoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        estadoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{publicacionBean.publicacion.estado}", String.class));
        htmlPanelGrid.getChildren().add(estadoValue);
        
        return htmlPanelGrid;
    }

	public Publicacion getPublicacion() {
        if (publicacion == null) {
            publicacion = new Publicacion();
        }
        return publicacion;
    }

	public void setPublicacion(Publicacion publicacion) {
        this.publicacion = publicacion;
    }

	public List<Imagen> getSelectedImagens() {
        return selectedImagens;
    }

	public void setSelectedImagens(List<Imagen> selectedImagens) {
        if (selectedImagens != null) {
            publicacion.setImagens(new HashSet<Imagen>(selectedImagens));
        }
        this.selectedImagens = selectedImagens;
    }

	public List<Transaccion> getSelectedTransaccions() {
        return selectedTransaccions;
    }

	public void setSelectedTransaccions(List<Transaccion> selectedTransaccions) {
        if (selectedTransaccions != null) {
            publicacion.setTransaccions(new HashSet<Transaccion>(selectedTransaccions));
        }
        this.selectedTransaccions = selectedTransaccions;
    }

	public List<Visita> getSelectedVisitas() {
        return selectedVisitas;
    }

	public void setSelectedVisitas(List<Visita> selectedVisitas) {
        if (selectedVisitas != null) {
            publicacion.setVisitas(new HashSet<Visita>(selectedVisitas));
        }
        this.selectedVisitas = selectedVisitas;
    }

	public List<Categoria> completeCategoria(String query) {
        List<Categoria> suggestions = new ArrayList<Categoria>();
        for (Categoria categoria : categoriaService.findAllCategorias()) {
            String categoriaStr = String.valueOf(categoria.getNombre() +  " "  + categoria.getDescripcion());
            if (categoriaStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(categoria);
            }
        }
        return suggestions;
    }

	public List<Usuario> completeUsuario(String query) {
        List<Usuario> suggestions = new ArrayList<Usuario>();
        for (Usuario usuario : usuarioService.findAllUsuarios()) {
            String usuarioStr = String.valueOf(usuario.getNombres() +  " "  + usuario.getApellidoPaterno() +  " "  + usuario.getApellidoMaterno() +  " "  + usuario.getSexo());
            if (usuarioStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(usuario);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        if (publicacion != null && publicacion.getImagens() != null) {
            selectedImagens = new ArrayList<Imagen>(publicacion.getImagens());
        }
        if (publicacion != null && publicacion.getTransaccions() != null) {
            selectedTransaccions = new ArrayList<Transaccion>(publicacion.getTransaccions());
        }
        if (publicacion != null && publicacion.getVisitas() != null) {
            selectedVisitas = new ArrayList<Visita>(publicacion.getVisitas());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllPublicacions();
        return "publicacion";
    }

	public String displayCreateDialog() {
        publicacion = new Publicacion();
        createDialogVisible = true;
        return "publicacion";
    }

	public String persist() {
        String message = "";
        if (publicacion.getCodigo() != null) {
            publicacionService.updatePublicacion(publicacion);
            message = "Successfully updated";
        } else {
            publicacionService.savePublicacion(publicacion);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllPublicacions();
    }

	public String delete() {
        publicacionService.deletePublicacion(publicacion);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllPublicacions();
    }

	public void reset() {
        publicacion = null;
        selectedImagens = null;
        selectedTransaccions = null;
        selectedVisitas = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }
}
